<template>
  <div>
    <!-- <h3>我的商城</h3> -->
    <van-search v-model="value" shape="round" background="#4fb78d" placeholder="请输入搜索关键词" show-action @search="onSearch"
      @cancel="onCancel" />
  </div>
  <div class="swipe">
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="(item, index) in images" :key="index">
        <img :src="item.img1" alt="轮播图" style="width: 100%; height: 200px;" />
      </van-swipe-item>
    </van-swipe>
  </div>
  <van-grid>
    <van-grid-item icon="good-job-o" text="热门推荐" />
    <van-grid-item icon="hot-o" text="本周热卖" />
    <van-grid-item icon="new-o" text="本周新卖" />
    <van-grid-item icon="shop-o" text="全部分类" />
  </van-grid>

  <ul>
    <li v-for="v in productArr" @click="$router.push('/detail?id=' + v.proid)">
      <img :src="v.img2" alt="">
      <h4 class="van-multi-ellipsis--l2">{{ v.proname }}</h4>
      <div class="cart" @click.stop="addCart(v)">加入购物车</div>
      <div class="buy">
        <span style="font-size: 18px; color: red;line-height: 30px;">￥{{ v.originprice }}</span>
        <van-button type="danger" class="buynow" size="small">直接购买</van-button>
      </div>
    </li>
  </ul>

</template>

<script>
import axios from 'axios';
import { productListRequest } from '../../api/homeApi'
import { addCartRequest } from '../../api/cartApi'
import { showSuccessToast } from 'vant';

export default {
  data() {
    return {
      value: '',
      searchList: [],
      images: [],
      productArr: [],
      id: localStorage.getItem('mallFrontUserid')
    }
  },
  methods: {

    onSearch(){
      console.log('search');
      this.$router.push('/search?word='+this.value)
    },
    onCancel(){
      console.log('cancel');
      this.value = ''
    },
    getImages() {
      axios.get('http://47.94.6.186:3001/api/pro/recommendlist', {
        params: {
          count: 1,
          limitNum: 4
        }
      }).then(res => {
        console.log(res.data);
        this.images = res.data.data;
      })
    },
    addCart(v) {
      console.log(v);
      addCartRequest(this.id, v.proid).then(res => {
        console.log(res.data);
        showSuccessToast(res.data.message)
      })
    }

  },
  mounted() {


    this.getImages();

    productListRequest().then(res => {
      console.log(res.data);
      if (res.data.code == 200) {
        this.productArr = res.data.data;
      }
    })

    // getSearchList({ count: 1, limitNum: 10, keyword: '' }).then(res => {
    //   console.log(res.data);
    // })
  }
}
</script>

<style scoped>
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 10px;
  line-height: height;
  text-align: center;
  background-color: #39a9ed;
}

.swipe {
  height: 200px;
}

ul {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  box-sizing: border-box;
}

li {
  list-style: none;
  background-color: #fff;
  width: 45%;
  margin-top: 10px;
  overflow: hidden;
}

li img {
  width: 100%;
  height: 50%;
}

li h4 {
  font-size: 14px;
  height: 30px;
}

.cart {
  height: 20px;
  font-size: 16px;
  text-align: right;
  color: #0872CE;
}

.buy {
  display: flex;
  justify-content: space-between;
  height: 30px;
  margin-top: 20px;
}

.buynow {

  font-size: 16px;
}

span {
  line-height: 30px;
}
</style>